package com.example.xunua_weather;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.xunua_weather.Sqliteutils.DBManager;



public class MoreActivity extends AppCompatActivity implements View.OnClickListener{
    TextView bgTv,cacheTv,versionTv,shareTv;
    RadioGroup exbgRg;
    ImageView backIv;
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        bgTv = findViewById(R.id.more_tv_exchangebg);
        cacheTv = findViewById(R.id.more_tv_cache);
        versionTv = findViewById(R.id.more_tv_version);
        shareTv = findViewById(R.id.more_tv_share);
        exbgRg = findViewById(R.id.more_rg);
        backIv = findViewById(R.id.more_back_iv);

        bgTv.setOnClickListener(this);
        cacheTv.setOnClickListener(this);
        shareTv.setOnClickListener(this);
        backIv.setOnClickListener(this);
        pref = getSharedPreferences("bg_sp",MODE_PRIVATE);
        setRGListener();

        versionTv.setText("当前版本 :      v"+getVersionCode(MoreActivity.this)+".00");
    }

//    切换壁纸的单选按钮的监听
    private void setRGListener() {
        exbgRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int bg=pref.getInt("bg",2);
                SharedPreferences.Editor edit1=pref.edit();
                Intent intent=new Intent();
                switch (checkedId){
                    case R.id.more_rb_green:
                        if (bg == 0) {
                            Toast.makeText(MoreActivity.this, "您选择的正是当前背景图片，无需改变", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit1.putInt("bg",0);
                        edit1.commit();
                        intent.setClass(MoreActivity.this,MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        break;
                    case R.id.more_rb_pink:
                        if (bg == 1) {
                            Toast.makeText(MoreActivity.this, "您选择的正是当前背景图片，无需改变", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit1.putInt("bg",1);
                        edit1.commit();
                        intent.setClass(MoreActivity.this,MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        break;
                    case R.id.more_rb_blue:
                        if (bg == 2) {
                            Toast.makeText(MoreActivity.this, "您选择的正是当前背景图片，无需改变", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        edit1.putInt("bg",2);
                        edit1.commit();
                        intent.setClass(MoreActivity.this,MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        break;
                }
                startActivity(intent);
            }
        });
    }

    /**
     * 获取本地apk的版本号
     * @param mContext
     * @return
     */
    public static int getVersionCode(Context mContext){
        int versionCode=0;
        //获取软件版本号，对应AndroidManifest.xml中的versionCode
        try {
            versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.more_tv_exchangebg:
                if (exbgRg.getVisibility()==View.GONE){
                    exbgRg.setVisibility(View.VISIBLE);
                }else {
                    exbgRg.setVisibility(View.GONE);
                }
                break;
            case R.id.more_tv_cache:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("提示信息")
                        .setMessage("确定要删除所有缓存么？").setPositiveButton("取消",null)
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DBManager.deleteAllInfo();
                                Toast.makeText(MoreActivity.this, "已清除全部缓存", Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent();
                                intent.setClass(MoreActivity.this,MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            }
                        });
                builder.create().show();
                break;
            case R.id.more_tv_share:
                Intent textIntent=new Intent(Intent.ACTION_SEND); //发送分享
                textIntent.setType("text/plain");
                textIntent.putExtra(Intent.EXTRA_TEXT,"猛男天气是一款超萌超可爱的天气预报软件，画面简约，播报天气清空非常精准，快来下载吧！");
                startActivity(Intent.createChooser(textIntent,"说天气"));
                break;
            case R.id.more_back_iv:
                finish();
                break;
        }
    }
}
